/*
 * @Author: zh 18812415351@163.com
 * @Date: 2023-08-22 11:47:54
 * @LastEditors: zh 18812415351@163.com
 * @LastEditTime: 2023-08-22 11:49:49
 * @FilePath: \manage_client\config\httpload.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import Request from '@/js_sdk/luch-request/index.js'
import {
	BASE_API,
	BASE_URL
} from '@/config/url.js'
// import qs from 'qs'
const http = new Request({
	baseURL: BASE_URL, //设置请求的base url
	timeout: 300000, //超时时长5分钟,
	header: {
		'Content-Type': 'application/json; charset=utf-8',
		'client':"wx_manage",
		'version':'v01',
		'token':''
	}
})

//请求拦截器
http.interceptors.request.use((config) => { // 可使用async await 做异步操作

	const token = uni.getStorageSync('token');
	if (token) {
		config.header.token = token;
	}

	if (config.method === 'POST') {
		config.data = JSON.stringify(config.data);
	}
	uni.showLoading({
		title: '加载中，请稍后....'
	});
	return config
}, error => {
	return Promise.resolve(error)
})

// 响应拦截器
http.interceptors.response.use((response) => {
	uni.hideLoading();
	if ((response.data.code == 50106 && response.data) || (response.data.code == 50107 && response.data)) {
		uni.clearStorageSync();
		uni.navigateTo({
			url: "/pages/login/login"
		})
		uni.showToast({
			title: response.data.message,
			icon: "none",
			duration: 2000
		});
	}
	return response
}, (error) => {
	/* //未登录时清空缓存跳转
	if (error.statusCode == 401) {
		uni.clearStorageSync();
		uni.switchTab({
			url: "/pages/home/index/index"
		})
	} */
	return Promise.resolve(error)
})
export default http;